package com.ruibang.glass.produce.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.material.MaterialManagement;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.MaterialFeedRecordReq;
import com.ruibang.glass.produce.entity.MaterialFeedRecord;
import com.ruibang.glass.produce.entity.MaterialWarehouse;
import com.ruibang.glass.produce.feign.MaterialApi;
import com.ruibang.glass.produce.mapper.MaterialFeedRecordMapper;
import com.ruibang.glass.produce.service.MaterialFeedRecordService;
import com.ruibang.glass.produce.service.MaterialWarehouseService;
import com.ruibang.glass.produce.util.ExcelStyleUtils;
import com.teaming.cloud.framework2.common.model.ResultBody;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-03-11  11:19
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class MaterialFeedRecordServiceImpl extends ServiceImpl<MaterialFeedRecordMapper, MaterialFeedRecord> implements MaterialFeedRecordService {

    @Autowired
    private MaterialWarehouseService materialWarehouseService;

    @Autowired
    private MaterialApi materialApi;

    @Override
    public void deleteById(String id) {
        lambdaUpdate().eq(MaterialFeedRecord::getId, id).set(MaterialFeedRecord::getIsDelete, Constants.ONE_STR).update();
    }

    @Override
    public void export(MaterialFeedRecordReq materialFeedRecordReq, HttpServletResponse response) {
        materialFeedRecordReq.setPageNo(-1);
        materialFeedRecordReq.setPageSize(-1);
        IPage<MaterialFeedRecord> page = this.pageQuery(materialFeedRecordReq);
        if (page != null && CollectionUtils.isNotEmpty(page.getRecords())) {
            List<MaterialFeedRecord> materialFeedRecordList = page.getRecords();
            try {
                EasyExcel.write(response.getOutputStream(), MaterialFeedRecord.class)
                        .registerWriteHandler(ExcelStyleUtils.createTableStyle())
                        .sheet("history_data").doWrite(materialFeedRecordList);
            } catch (IOException e) {
                log.error("数据导出失败：{}", e.getMessage());
            }
        }
    }

    @Override
    public void insert(MaterialFeedRecord materialFeedRecord) {
        //料仓
        String materialWarehouseId = materialFeedRecord.getMaterialWarehouseId();
        if (StringUtil.isNotEmpty(materialWarehouseId)) {
            MaterialWarehouse materialWarehouse = materialWarehouseService.getById(materialWarehouseId);
            if (materialWarehouse != null) {
                materialFeedRecord.setMaterialWarehouseName(materialWarehouse.getMaterialWarehouseName());
            }
        }
        //物料
        String materialId = materialFeedRecord.getMaterialId();
        if (StringUtil.isNotEmpty(materialId)) {
            ResultBody<MaterialManagement> resultBody = materialApi.getById(materialId);
            if (resultBody != null && resultBody.getData() != null) {
                MaterialManagement materialManagement = resultBody.getData();
                materialFeedRecord.setMaterialName(materialManagement.getItemName());
            }
        }
        //保存
        materialFeedRecord.setIsDelete(Constants.ZERO_STR);
        this.save(materialFeedRecord);
    }

    /**
     * @Description: 根据日期获取投料记录
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-07 14:44
     */
    @Override
    public List<MaterialFeedRecord> getByDate(LocalDate date) {
        return baseMapper.getByDate(date);
    }

    @Override
    public void edit(MaterialFeedRecord materialFeedRecord) {
        lambdaUpdate().eq(MaterialFeedRecord::getId, materialFeedRecord.getId())
                .set(MaterialFeedRecord::getMaterialWarehouseId, materialFeedRecord.getMaterialWarehouseId())
                .set(MaterialFeedRecord::getMaterialWarehouseName, materialFeedRecord.getMaterialWarehouseName())
                .set(MaterialFeedRecord::getMaterialId, materialFeedRecord.getMaterialId())
                .set(MaterialFeedRecord::getMaterialName, materialFeedRecord.getMaterialName())
                .set(MaterialFeedRecord::getBatchNumber, materialFeedRecord.getBatchNumber())
                .set(MaterialFeedRecord::getFeedTime, materialFeedRecord.getFeedTime())
                .set(MaterialFeedRecord::getFeedNum, materialFeedRecord.getFeedNum()).update();

    }

    @Override
    public Page<MaterialFeedRecord> pageQuery(MaterialFeedRecordReq materialFeedRecordReq) {
        QueryWrapper<MaterialFeedRecord> qw = new QueryWrapper<>();
        qw.lambda().eq(MaterialFeedRecord::getIsDelete, Constants.ZERO_STR);
        qw.lambda().like(StringUtil.isNotEmpty(materialFeedRecordReq.getSiloName()), MaterialFeedRecord::getMaterialWarehouseName, materialFeedRecordReq.getSiloName());
        if (materialFeedRecordReq.getFeedStartTime() != null && materialFeedRecordReq.getFeedEndTime() != null) {
            qw.lambda().ge(MaterialFeedRecord::getFeedTime, materialFeedRecordReq.getFeedStartTime());
            qw.lambda().le(MaterialFeedRecord::getFeedTime, materialFeedRecordReq.getFeedEndTime());
        }
        return this.page(new Page<>(materialFeedRecordReq.getPageNo(), materialFeedRecordReq.getPageSize()), qw.lambda());
    }
}
